package cn.exrick.service.impl;

import cn.exrick.common.exception.XmallException;
import cn.exrick.mapper.TbQuestionCatMapper;
import cn.exrick.pojo.TbQuestionCat;
import cn.exrick.pojo.TbQuestionCatExample;
import cn.exrick.service.QuestionCatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 */
@Service
public class QuestionCatServiceImpl implements QuestionCatService {

    @Autowired
    private TbQuestionCatMapper tbQuestionCatMapper;

    @Override
    public int save(TbQuestionCat tbQuestionCat) {
        if(getByName(tbQuestionCat.getName())!=null){
            throw new XmallException("该分类名已存在");
        }
        tbQuestionCat.setCreated(new Date());
        tbQuestionCat.setUpdated(new Date());
        if(tbQuestionCatMapper.insert(tbQuestionCat) != 1){
            throw new XmallException("添加问答分类失败");
        }
        return 1;
    }

    @Override
    public int update(TbQuestionCat tbQuestionCat) {
        if(!checkNameForEdit(tbQuestionCat.getId(), tbQuestionCat.getName())){
            throw new XmallException("该问答分类名已存在");
        }
        tbQuestionCat.setUpdated(new Date());
        if(tbQuestionCatMapper.updateByPrimaryKeySelective(tbQuestionCat)!=1){
            throw new XmallException("更新问答分类失败");
        }
        return 1;
    }

    @Override
    public int delete(long id) {
        int count = tbQuestionCatMapper.selectUsedCount(id);
        if(count != 0){
            return 0;
        }
        if(tbQuestionCatMapper.deleteByPrimaryKey(id)!=1){
            throw new XmallException("删除问答分类失败");
        }
        return 1;
    }

    @Override
    public List<TbQuestionCat> getAll() {
        TbQuestionCatExample example = new TbQuestionCatExample();
        List<TbQuestionCat> list = tbQuestionCatMapper.selectByExample(example);
        if(list == null){
            throw new XmallException("获取问答分类列表失败");
        }
        return list;
    }

    @Override
    public TbQuestionCat getByName(String name) {
        TbQuestionCatExample example = new TbQuestionCatExample();
        TbQuestionCatExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(name);
        List<TbQuestionCat> list=new ArrayList<>();
        try {
            list = tbQuestionCatMapper.selectByExample(example);
        }catch (Exception e){
            throw new XmallException("通过问答分类名获取分类失败");
        }
        if(!list.isEmpty()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public boolean checkNameForEdit(long id, String name) {
        TbQuestionCat tbQuestionCat = tbQuestionCatMapper.selectByPrimaryKey(id);
        TbQuestionCat newQuestionCat = null;
        if(tbQuestionCat == null){
            throw new XmallException("通过ID获取问答分类失败");
        }
        if(!tbQuestionCat.getName().equals(name)){
            newQuestionCat = getByName(name);
        }
        return newQuestionCat == null;
    }

}
